package edu.nf.api.controller.rider;

import edu.nf.api.controller.BaseController;

import edu.nf.waimai.dao.rider.KnightDAO;
import edu.nf.waimai.dao.rider.impl.KnightDAOImpl;
import edu.nf.waimai.dao.rider.impl.RiderDAOImpl;
import edu.nf.waimai.model.order.OrderDTO;
import edu.nf.waimai.model.ridertakeorders.MerchantOrders;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import java.util.List;

/**
 * @Date 2023-06-25
 * @Author 陈
 */
public class RiderController extends BaseController {

    /**
     * 商家地址
     * @return
     */
    @RequestMapping("/rider/business/BusinessAddress")
    public View RiderBusinessAddress() {
        KnightDAO knightDao = new KnightDAOImpl();
        List<MerchantOrders> list = knightDao.businessOrdersInquire();
        return new JsonView(list);
    }


    /**
     * 骑手接单
     * @param rid
     * @param oid
     * @return
     */
    @RequestMapping("/rider/business/riderUpdate")
    public View RiderTakeOrders(@RequestParam("riderId") int rid,
                                @RequestParam("orderId") int oid) {
        KnightDAO knightDao = new KnightDAOImpl();
        int row = knightDao.OrdersRidUpdate(rid,oid);
        return new JsonView(row);
    }

    /**
     * 完成订单
     * @param sta
     * @param rid
     * @return
     */
    @RequestMapping("/rider/business/riderFinish")
    public View RiderFinish (@RequestParam("status") int sta,
                             @RequestParam("riderId") int rid) {
        KnightDAO knightDao = new KnightDAOImpl();
        int row  = knightDao.OrdersStaUpdate(sta,rid);
        return new JsonView(row);
    }

    /**
     * 查询全部的订单
     * @param orderStatus
     * @param orderType
     * @return
     */
    @RequestMapping("/rider/listOrdersByTypeAndState")
    public View listOrdersByTypeAndState(@RequestParam("orderStatus") int orderStatus,@RequestParam("orderType") String orderType){
        RiderDAOImpl riderDAO = new RiderDAOImpl();
        List<OrderDTO> orderDTOS = riderDAO.listOrdersByTypeAndState(orderType, orderStatus);
        return new JsonView(success(orderDTOS));
    }

    /**
     * 修改订单状态
     * @param orderId 订单id
     * @param orderStatus 状态值
     * @return
     */
    @RequestMapping("/rider/updateRiderOrder")
    public View updateRiderOrder(@RequestParam("orderId") String orderId,@RequestParam("orderStatus") int orderStatus,@RequestParam("riderId") int riderId){
        System.out.println("orderId = " + orderId);
        System.out.println("orderStatus = " + orderStatus);
        System.out.println("riderId = " + riderId);
        RiderDAOImpl riderDAO = new RiderDAOImpl();
        return new JsonView(success(riderDAO.updateRiderOrder(orderId,orderStatus,riderId)));
    }

    /**
     * 通过骑手id和订单状态查询订单
     * @param riderId
     * @param orderState
     * @return
     */
    @RequestMapping("/rider/listRiderOrder")
    public View listRiderOrder(@RequestParam("riderId")int riderId,@RequestParam("orderState")int orderState){
        System.out.println("riderId = " + riderId + ", orderState = " + orderState);
        RiderDAOImpl riderDAO = new RiderDAOImpl();
        return new JsonView(success(riderDAO.listOrdersByIdAndState(riderId,orderState)));
    }
}
